package com.atguigu.gmall.activity.controller;

import com.atguigu.gmall.activity.model.SeckillGoods;
import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-03-17 14:20
 */
@RestController
@RequestMapping("/api/activity")
public class SeckillGoodsApiController {

    @Autowired
    private SeckillGoodsService seckillGoodsService;


    /**
     * 查询Redis中当日秒杀商品列表
     *
     * @return
     */
    @GetMapping("/inner/seckill/findAll")
    public Result getSeckillGoodsList() {
        List<SeckillGoods> seckillGoodsList = seckillGoodsService.getSeckillGoodsList();
        return Result.ok(seckillGoodsList);
    }

    /**
     * 查询指定秒杀商品信息
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/seckill/getSeckillGoods/{skuId}")
    public Result getSeckillGoods(@PathVariable("skuId") Long skuId) {
        SeckillGoods seckillGoods = seckillGoodsService.getSeckillGoods(skuId);
        return Result.ok(seckillGoods);
    }


    /**
     * 点击秒杀按钮前,为当前登录用户生成购买指定商品抢购码,避免非法抢购
     *
     * @param skuId
     * @return
     */
    @GetMapping("/seckill/auth/getSeckillSkuIdStr/{skuId}")
    public Result getSeckillSkuIdStrBuyCode(HttpServletRequest request, @PathVariable("skuId") Long skuId) {
        //1.获取登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        if (userId == null) {
            return Result.fail().message(ResultCodeEnum.LOGIN_AUTH.getMessage());
        }
        //2.调用业务逻辑生成抢购码
        String buyCode = seckillGoodsService.getSeckillBuyCode(userId, skuId);
        return Result.ok(buyCode);
    }


    /**
     * 秒杀请求入队-将用户购买意向发送消息队列
     *
     * @param skuId   商品ID
     * @param buyCode 抢购码
     * @return
     */
    @PostMapping("/seckill/auth/seckillOrder/{skuId}")
    public Result seckillRequestToQueue(HttpServletRequest request, @PathVariable("skuId") Long skuId, @RequestParam("skuIdStr") String buyCode) {
        //1.获取登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        if (userId == null) {
            return Result.fail().message(ResultCodeEnum.LOGIN_AUTH.getMessage());
        }
        //2.调用业务层秒杀入队处理
        return seckillGoodsService.seckillRequestToQueue(userId, skuId, buyCode);
    }


    /**
     * 检查用户秒杀指定商品结果
     * @param skuId 商品ID
     * @return
     */
    @GetMapping("/seckill/auth/checkOrder/{skuId}")
    public Result checkSeckillResult(HttpServletRequest request, @PathVariable("skuId") Long skuId){
        String userId = AuthContextHolder.getUserId(request);
        if (userId == null) {
            return Result.fail().message(ResultCodeEnum.LOGIN_AUTH.getMessage());
        }
        return seckillGoodsService.checkSeckillResult(userId, skuId);
    }


    /**
     * 渲染秒杀订单确认页面相关数据
     * @param request
     * @return
     */
    @GetMapping("/seckill/auth/trade/{skuId}")
    public Result seckillOrderTrade(HttpServletRequest request, @PathVariable("skuId") Long skuId){
        String userId = AuthContextHolder.getUserId(request);
        if (userId == null) {
            return Result.fail().message(ResultCodeEnum.LOGIN_AUTH.getMessage());
        }
        Map<String, Object> mapResult = seckillGoodsService.seckillOrderTrade(userId, skuId);
        return Result.ok(mapResult);
    }


    /**
     * 保存秒杀订单
     * @param orderInfo
     * @return 跟以前支付对接 响应保存后秒杀订单ID
     */
    @PostMapping("/seckill/auth/submitOrder")
    public Result submitSeckillOrder(HttpServletRequest request, @RequestBody OrderInfo orderInfo){
        String userId = AuthContextHolder.getUserId(request);
        if (userId == null) {
            return Result.fail().message(ResultCodeEnum.LOGIN_AUTH.getMessage());
        }
        orderInfo.setUserId(Long.valueOf(userId));
        Long orderId = seckillGoodsService.submitSeckillOrder(orderInfo);
        return Result.ok(orderId);
    }
}
